<?PHP
class customerEntity
{
	public static function GetList()
	{   //返回已审核的客户列表
		return $_ENV['db']->fetchAll("SELECT * FROM customer WHERE state='1'" );
	}
	
	/*[返回某客户下操作人]*/
	//状态 1 正常 2待审核
	public static function GetUserList($cid,$state='')
	{
		$state = $state?" state='" .$state. "' ":' 1=1 ';
		return $_ENV['db']->fetchAll("SELECT * FROM customer_leader WHERE customer_id='{$cid}' and " . $state);
	}
	
	/*[判断客户用户是否存在]*/
	public static function IsUserExist($uname)
	{	
		 $row = $_ENV['db']->fetchCol("SELECT username FROM customer_leader WHERE `username` = '{$uname}'");
		 if ($row) 
		 {return true;}
		 else
		 {
			return false;
		 }
	}
	
	
	/*[判断客户用户是否存在]*/
	public static function IsUserExists($realname,$uname)
	{	
		 $row = $_ENV['db']->fetchCol("SELECT username FROM customer_leader WHERE `username` = '{$uname}'");
		 if ($row) 
		 {return true;}
		 else
		 {
		 	$row2 = $_ENV['db']->fetchCol("SELECT realname FROM customer_leader WHERE `realname` = '{$realname}'");
			if ($row2) 
			 {return true;}
			 else
			 {
				 return false;
			 }
		 }
	}
	/*[判断客户用户是否存在]*/
	public static function IsUserExistsByID($realname,$uname,$id)
	{	
		 $row = $_ENV['db']->fetchCol("SELECT username FROM customer_leader WHERE `username` = '{$uname}'  and leader_id!='{$id}'");
		 if ($row) 
		 {return true;}
		 else
		 {
		 	$row2 = $_ENV['db']->fetchCol("SELECT realname FROM customer_leader WHERE `realname` = '{$realname}'  and leader_id!='{$id}'");
			if ($row2) 
			 {return true;}
			 else
			 {
				 return false;
			 }
		 }
	}
	
	/*[判断客户是否存在]*/
	public static function IsExists($customer)
	{	
		 $row = $_ENV['db']->fetchCol("SELECT customer_name FROM customer WHERE `customer_name` = '{$customer}'");
		 if ($row) 
		 {return true;}
		 else
		 {return false;}
	}
	/*[判断客户是否存在]*/
	public static function IsExistsByID($customer,$cid)
	{	
		 $row = $_ENV['db']->fetchCol("SELECT customer_name FROM customer WHERE `customer_name` = '{$customer}' and customer_id!='{$cid}'");
		 if ($row) 
		 {return true;}
		 else
		 {return false;}
	}
	
	public static function AddUser($data)
	{
		$data['state'] = '2';   //后台添加 默认是 已审核
		$data['created_time'] = $_ENV['time'];
		$data['password'] = md5($data['password']);
		return InsertTable('customer_leader', saddslashes($data));
	}
	
	public static function Add($data)
	{
		$data['state'] = '2';   //默认是未审核
		$data['created_time'] = $_ENV['time'];
		return InsertTable('customer', saddslashes($data));
	}
	
	public static function GetRow($cid)
	{
		$customer = $_ENV['db']->fetch("SELECT * FROM `customer` WHERE `customer_id` = '{$cid}'");
        if (!$customer) $this->showMessage('找不到指定客户信息',-404);
		return $customer;
	}
	
	public static function GetLeaderRow($lid)
	{
		$leader = $_ENV['db']->fetch("SELECT * FROM `customer_leader` WHERE `leader_id` = '{$lid}'");
		return $leader;
	}
	
	public static function GetUserRow($uid)
	{
		$user = $_ENV['db']->fetch("SELECT * FROM `customer_leader` WHERE `leader_id` = '{$uid}'");
        if (!$user) $this->showMessage('找不到指定客户信息',-404);
		return $user;
	}
	public static function Update($data)
	{
		return UpdateTable('customer', saddslashes($data),array('customer_id' => $data['customer_id']));
	}
	public static function UpdateUser($data)
	{
		return UpdateTable('customer_leader', saddslashes($data),array('leader_id' => $data['leader_id']));
	}
	
	
	//根据ID删除客户及下属的用户
	public static function DeteteByID($cid)
	{
		DeteteTable('customer_leader',array('customer_id' => $cid));
		DeteteTable('customer',array('customer_id' => $cid));
		return true;
	}
	
	
	
	
	public static function DeteteUserByID($uid)
	{
		return DeteteTable('customer_leader',array('leader_id' => $uid));
	}
	public static function SetUserEnable($uid,$enable)
	{
		return $_ENV['db']->runSql("UPDATE `customer_leader` SET `enable` = '{$enable}' WHERE `leader_id` = '{$uid}'");
	}
	
	//根据客户ID返回发票列表
	public static function GetFaPiaoList($cid)
	{
		return $_ENV['db']->fetchAll("SELECT * FROM `fapiao` where uid = '{$cid}'");
	}
	//根据客户ID返回收获地址列表
	public static function GetReceiveList($cid)
	{
		return $_ENV['db']->fetchAll("SELECT * FROM `customer_receive` WHERE `customer_id` = '{$cid}'");
	}
	
	
	/*[返回收货地址]*/
	public static function GetAddressRow($cid,$rid)
	{	
		return $_ENV['db']->fetch("SELECT * FROM `customer_receive` WHERE `customer_id` = '{$cid}' and  `cr_id` = '{$rid}'");
	}
	
	/*[返回发票]*/
	public static function GetFaPiaoRow($cid,$fid)
	{	
		 return  $_ENV['db']->fetch("SELECT * FROM `fapiao` WHERE `uid` = '{$cid}' and fid = '{$fid}'");
	}
	
}
?>